package cn.android.mydata2.dao;


import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.List;
import cn.android.mydata2.entities.TrafficModel;

/**
 * Created by Administrator on 2016/10/30.
 */

public class TrafficDao {

    DbManager db;

    public TrafficDao() {
        DbManager.DaoConfig con = new DbManager.DaoConfig().setDbName("traffic")
                .setDbVersion(1);
        db = x.getDb(con);
    }

    public void save(TrafficModel model) {
        // wifi cell total

        try {
            db.saveOrUpdate(model);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public TrafficModel getByUid(int uid){
        try {
            TrafficModel m = db.findById(TrafficModel.class, uid);
            return m;
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;

    }

    public static String dataSizeFormat(long size) {
        DecimalFormat formater = new DecimalFormat("#.#");
        double size1 = (double) size;
        if (size1 < 1024 * 1024) {
            double kSize = (size1 / 1024);
            return formater.format(kSize) + "KB";
        } else if (size1 < 1024 * 1024 * 1024) {
            double mSize = size1 / (1024 * 1024);
            return formater.format(mSize) + "MB";
        } else {
            double gSize = size1 / (1024 * 1024 * 1024);
            return formater.format(gSize) + "GB";
        }
    }
//
    public void close(){
//        try {
//            db.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    public List<TrafficModel> getAll() {
        try {
            return db.findAll(TrafficModel.class);
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    public TrafficModel getById(int id) {
        try {
            return db.findById(TrafficModel.class, id);
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;

    }
}
